package com.choosefine.it.basedata.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.Street;
import com.choosefine.it.basedata.service.StreetService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.StreetUtils;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;

/**
 * @author 潘钱钦 2017下午9:12:56
 */
@RestController
@RequestMapping("/street")
public class StreetController extends BaseController {
	@Autowired
	private StreetService streetService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value="/insert/{fileName}",method=RequestMethod.GET)
	@ApiImplicitParam(name = "fileName", value = "json文件名(不需要传入.json，只需前缀)", required = true, dataType = "String", paramType = "path")
	public String insert(@PathVariable String fileName) {
		String path="D:\\";
		String filePath=path+fileName+".json";
		String JsonContext = new StreetUtils().ReadFile(filePath);
		Gson gson = new Gson();
		Map<String, Object> map = new HashMap<String, Object>();
		map = gson.fromJson(JsonContext, map.getClass());
		Set<String> key = map.keySet();
		Street s = null;
		Integer isSuccess=null;
		Integer flag=0;
		for (String str : key) {
			s = new Street();
			s.setStreetId(Integer.valueOf(str));
			s.setStreet((String) map.get(str));
			s.setFather(Integer.valueOf(str.substring(0, 6)));
			isSuccess = streetService.insertStreet(s);
			flag=flag+isSuccess;
		}
		if (flag.intValue() == key.size()) {
			return responseSuccess(isSuccess, ResultCode.SUCCESS, "导入成功");
		}else{
		return responseFail("导入失败");
		}
	}
}
